package com.example.dormitoryscoresystem.service;

import com.example.dormitoryscoresystem.entity.RectificationNotice;
import com.example.dormitoryscoresystem.entity.ScoreRecord;
import com.example.dormitoryscoresystem.repository.RectificationRepository;
import com.example.dormitoryscoresystem.repository.ScoreRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;

import java.time.LocalDateTime;

@Service
public class ScoreService {

    @Autowired
    private ScoreRepository scoreRepository;

    @Autowired
    private RectificationRepository rectificationRepository;

    public void submitScore(String dormId, int score, String remarks) {
        // 1. 保存评分
        ScoreRecord scoreRecord = new ScoreRecord();
        scoreRecord.setDormId(dormId);
        scoreRecord.setScore(score);
        scoreRecord.setRemarks(remarks);

        // 将 Timestamp 转换为 LocalDateTime
        scoreRecord.setCreatedTime(LocalDateTime.now()); // 使用 LocalDateTime

        scoreRepository.save(scoreRecord);  // 使用 save 方法保存

        // 2. 分数小于 60，自动生成整改通知
        if (score < 60) {
            RectificationNotice notice = new RectificationNotice();
            notice.setDormId(dormId);
            notice.setScore(score);
            notice.setStatus("待整改");
            notice.setCreateTime(LocalDateTime.now()); // 使用 LocalDateTime
            rectificationRepository.save(notice);
        }
    }

}
